package com.hup.minicontroller.common.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Build.VERSION_CODES;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

/**
 * @author hugan
 * @date 2021/5/13
 */
public abstract class ActivityUtil {
    private static final String TAG = ActivityUtil.class.getSimpleName();

    /**
     * app全屏,隐藏状态栏和虚拟按键
     */
    @SuppressLint("ObsoleteSdkInt")
    @SuppressWarnings("deprecation")
    public static void switchFullScreen(Activity activity, boolean isShow) {
        //Log.i(TAG, "switchFullScreen: " + Build.VERSION.SDK_INT);
        Window window = activity.getWindow();
        if (Build.VERSION.SDK_INT >= VERSION_CODES.R) {//>=30
            Log.w(TAG, "switchFullScreen: 未适配", new Throwable());
        } else if (Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT) {//>=19,4.4
            /*
            https://developer.android.google.cn/training/system-ui/immersive?hl=en
            分三种模式:前两种就是鸡肋! 一定是用IMMERSIVE_STICKY
            百度一堆坑爹的都只加了两个flag,会导致某些设备不能隐藏顶部状态栏或虚拟按键
             */
            window.getDecorView().setSystemUiVisibility(isShow ?
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    :
                    View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
            );
        } else if (Build.VERSION.SDK_INT > VERSION_CODES.HONEYCOMB) {//>11,未验证!!
            View v = window.getDecorView();
            v.setSystemUiVisibility(isShow ? View.VISIBLE : View.GONE);
        }
    }

    /**
     * 隐藏状态栏
     */
    public static void switchSystemBar(Activity activity, boolean isShow) {
        Log.e(TAG, "switchSystemBar: 未完成!!");
        Window window = activity.getWindow();
        if (isShow) {
            window.clearFlags(LayoutParams.FLAG_FULLSCREEN);
        } else {
            window.addFlags(LayoutParams.FLAG_FULLSCREEN);
        }
    }

}
